<!DOCTYPE html>
<html>
<head>

    <script type="text/javascript" charset="utf-8" >
  window.WebSocket = window.WebSocket || window.MozWebSocket;

 if (!window.WebSocket){
     window.WebSocket = window.MozWebSocket;
 };

 var websocket;

 function send() {
    var name = document.querySelector("input[name=msg]").value;
    if(websocket != null) {
        websocket.send(name);
    }
 }

 function connect() {
    if(window.WebSocket){
        websocket = new WebSocket("ws://localhost:8999/websocket");

	    websocket.onmessage = function(event){
			var ta = document.getElementById('responseText');
			ta.value += event.data+"\r\n";
	    };

	    websocket.onopen = function(event){

			var ta = document.getElementById('responseText');
			ta.value = "打开WebSoket 服务正常，浏览器支持WebSoket!"+"\r\n";

	    };

	    websocket.onclose = function(event){

			var ta = document.getElementById('responseText');
			ta.value = "";
			ta.value = "WebSocket 关闭"+"\r\n";
	    };
    }else{
		alert("您的浏览器不支持WebSocket协议！");
    }
 }
</script>
</head>
<body>
<button onclick="connect()">连接服务器</button>
<label for="msg">消息内容:</label>
<input type="text" id="msg" name="msg" />
<button onclick="send()">Send</button><br/>
<textarea id="responseText" style="margin: 0px; width: 414px; height: 126px;color:red"></textarea>
</body>
</html>
